<template>
    <mescroll-uni ref="mescrollRef" @init="mescrollInit" :height="height" :down="downOption" @down="downRefresh"
        :up="upOption" @up="getData">
        <view class="cost-list">
            <cost-list-item :type="type" v-for="(item, index) in datas" :item="item" :index="index"
                :key="index"></cost-list-item>
        </view>
    </mescroll-uni>
</template>

<script>
import costListItem from "./cost-list-item.vue"
import { getSaleDetailPage } from "@/service/api/erp.js"
import { mapState } from "vuex"

export default {
    components: {
        costListItem,
    },
    props: {
        height: {
            type: String,
            default: "300px",
        },
        type: {
            type: String / Number,
            default: 2,
        },
        code: {
            type: String,
            default: '',
        }
    },
    watch: {
        type(newVal) {
            this.param.type = newVal
        },
        code(newVal) {
            this.param.code = newVal
        },
    },
    computed: {
        ...mapState("report", ["startDate", "endDate", "selectedSalesman", "selectedCustoms", "selectedProject", "selectedMarketing", "selectedWarehouse", "selectedMaterial", "selectedDept", "dataZero"]),
    },
    data() {
        return {
            datas: [],
            allLength: 0,
            param: {
                code: this.code,
                type: this.type,
                pageNumber: 0,
                pageSize: 0,
                sortType: 0, // 0：不排序；1：升序；2：降序
                sortColumn: "", // Total 净销售额  MaoriProfit 净利润
            },
            downOption: {
                use: true,
            },
            isLoaded: false,
        }
    },
    methods: {
        //获取列表数据
        getData(page) {
            let param = this.param
            param.pageNumber = page.num
            param.pageSize = page.size
            if (this.selectedSalesman) {
                param.eCodeArr = [this.selectedSalesman.UserCode]
            } else {
                delete param.eCodeArr
            }
            if (this.selectedCustoms.length > 0) {
                param.bCodeArr = []
                this.selectedCustoms.forEach((e) => {
                    param.bCodeArr.push(e.UserCode)
                })
            } else {
                delete param.bCodeArr
            }
            if (this.selectedProject.length > 0) {
                param.projectArr = []
                this.selectedProject.forEach((e) => {
                    param.projectArr.push(e.ProjectNo)
                })
            } else {
                delete param.projectArr
            }
            if (this.selectedMarketing.length > 0) {
                param.organizationsArr = []
                this.selectedMarketing.forEach((e) => {
                    param.organizationsArr.push(e.OrganizationsNo)
                })
            } else {
                delete param.organizationsArr
            }
            if (this.selectedWarehouse.length > 0) {
                param.stockArr = []
                this.selectedWarehouse.forEach((e) => {
                    param.stockArr.push(e.StockInfoNo)
                })
            } else {
                delete param.stockArr
            }
            if (this.selectedMaterial.length > 0) {
                param.materialArr = []
                this.selectedMaterial.forEach((e) => {
                    param.materialArr.push(e.FNUMBER)
                })
            } else {
                delete param.materialArr
            }
            if (this.selectedDept.length > 0) {
                param.deptArr = []
                this.selectedDept.forEach((e) => {
                    param.deptArr.push(e.DeptInfoNo)
                })
            } else {
                delete param.deptArr
            }
            param.dataZero = this.dataZero
            // if(this.code){
            // 	param.code = this.code
            // }else{
            // 	delete param.code
            // }
            getSaleDetailPage({
                ...param,
                startDate: this.startDate,
                endDate: this.endDate,
            }).then((res) => {
                if (res.State == 1) {
                    this.mescroll.endByPage(res.Data.length, res.TotalAmout)
                    this.datas = this.datas.concat(res.Data)
                    this.allLength = res.RecordAmout
                    this.isLoaded = true
                    this.$emit("totalBack", res.RecordAmout)
                } else {
                    this.mescroll.endBySize(0, 0)
                }
            })
        },
        //刷新
        refresh(param) {
            if (param) {
                this.param = param
            }
            this.downRefresh()
        },
        //刷新
        downRefresh() {
            this.datas = []
            this.mescroll.resetUpScroll()
        },
    },

}
</script>

<style lang="scss">
.cost-list {
    // background: $color-white;
    padding: 12rpx 24rpx 24rpx 24rpx;
}
</style>
